package com.canyou.archives;


import com.canyou.api.ResponseBO;
import com.canyou.archives.model.Category;
import com.canyou.archives.model.dto.CategoryDTO;
import com.canyou.archives.model.dto.CategoryPageDTO;
import com.canyou.archives.model.vo.PageVO;
import com.canyou.archives.service.CategoryService;
import com.canyou.utils.DownloadUtils;
import com.canyou.utils.PathUtils;
import com.canyou.utils.StringUtils;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.canyou.api.BaseApiController;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

import java.io.File;
import java.io.IOException;
import java.util.List;

import static com.canyou.api.ResponseBO.*;

/**
 * <p>
 * 设备分类 前端控制器
 * </p>
 *
 * @author canyou
 * @since 2021-12-07
 */
@Api(tags = "设备分类管理")
@ApiSort(2001)
@RestController
@RequestMapping("/category")
public class CategoryController extends BaseApiController {
    @Autowired
    private CategoryService categoryService;

    @ApiOperation(value = "新增编辑设备分类")
    //@PreAuthorize("hasPerm('role:add','role:edit')")
    @PostMapping("/save")
    public ResponseBO saveOrUpdateCategory(@ApiParam(name = "categoryDTO", type = "CategoryDTO", value = "设备分类新增编辑数据", required = true)
                               @Valid
                               @NotNull(message = "设备分类信息不能为空")
                               @RequestBody CategoryDTO categoryDTO) {
        //调用逻辑
        Category category=categoryService.saveOrUpdateCategory(categoryDTO);
        //返回结果给前端
        if(null==category){
            return fail("操作失败！");
        }
        return ok(category);
    }

    @ApiOperation(value = "条件分页查询分页")
    //@PreAuthorize("hasPerm('role')")
    @PostMapping("/get_page/{current}/{pageSize}")
    public ResponseBO getPageList(@ApiParam(name = "current",value = "当前页",required = true)
                                      @PathVariable("current")Integer current,
                                  @ApiParam(name = "pageSize",value = "每页数量",required = true)
                                      @PathVariable("pageSize")Integer pageSize,
                                               @RequestBody(required = false) CategoryPageDTO categoryPageDTO) {
        PageVO page= categoryService.getPageList(current,pageSize,categoryPageDTO);
        return ok(page);
    }

    @ApiOperation(value = "根据id查询分类")
    @ApiImplicitParam(name = "id", paramType = "query", value = "设备分类id", dataType = "int", required = true, example = "1")
    //@PreAuthorize("hasPerm('role')")
    @GetMapping("/get_info")
    public ResponseBO<Category> getCategoryInfo(@NotNull(message = "分类id不能为空")
                                        @RequestParam(required = false) Long id) {
        return ok(categoryService.getById(id));
    }

    @ApiOperation(value = "查询全部分类")
    //@PreAuthorize("hasPerm('role')")
    @GetMapping("/get_all")
    public ResponseBO<List<Category>> getCategoryAll() {
        //返回全部分类集合
        PageVO page= categoryService.getCategoryAll();
        if(StringUtils.isNotNull(page)){
            return ok(page.getRows());
        }
        return error();
    }

    @ApiOperation(value = "设备分类Excel导出")
    //@PreAuthorize("hasPerm('user:export')")
    @GetMapping(value = "/export_excel")
    public ResponseBO exportExcel() {
       Boolean flag= categoryService.exportExcel(this.response);
        if(flag){
            return ok();
        }
        return error();
    }

    @ApiOperation(value = "设备分类导入模板下载")
    //@PreAuthorize("hasPerm('user:import')")
    @GetMapping(value = "/download_template")
    public void downloadTemplate() throws IOException {
        File file = PathUtils.getClassPathResourceFile("template/import/category-template.xlsx");
        String fileName = "设备分类导入模板.xlsx";
        DownloadUtils.downFile(file, fileName, "application/vnd.ms-excel", response);
    }

    @ApiOperation(value = "设备分类Excel导入")
    @ApiImplicitParam(name = "file", value = "待上传的设备分类excel文件", paramType = "form",
            dataType = "MultipartFile", required = true)
    //@PreAuthorize("hasPerm('user:import')")
    @PostMapping(value = "/import_excel")
    public ResponseBO importExcel(@NotNull(message = "用户excel文件不能为空")
                                                @RequestPart(value = "file") MultipartFile file) throws Exception {
        Boolean flag=categoryService.importExcel(file);
        if(flag){
            return ok();
        }
        return error();
    }

}
